<template>
  <div>
    <h2>优惠劵添加</h2>
    <hr />
    <table border="1">
      <tr>
        <td align="right">门店:</td>
        <td>
          <select v-model="state.mId">
            <option value="0">请选择</option>
            <option :value="item.mId" v-for="item in list">
              {{ item.mName }}
            </option>
          </select>
        </td>
      </tr>
      <tr>
        <td align="right">优惠劵名称:</td>
        <td>
          <input type="text" v-model="state.yName" />
        </td>
      </tr>
      <tr>
        <td align="right">优惠劵类型:</td>
        <td>
          <input type="text" v-model="state.yType" />
        </td>
      </tr>
      <tr>
        <td align="right">优惠劵面值:</td>
        <td>
          <input type="number" v-model="state.ymz" />
        </td>
      </tr>
      <tr>
        <td align="right">优惠劵消费:</td>
        <td>
          <input type="number" v-model="state.yxf" />
        </td>
      </tr>
      <tr>
        <td align="right">优惠劵有效期:</td>
        <td>
          <input type="text" v-model="state.yxq" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="button" value="优惠劵添加" @click="YHJAdd" />
        </td>
        <td>
          <input type="button" value="优惠劵列表" @click="YHJShow" />
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const route = useRoute();

let data: any = reactive({
  state: {
    mId: '0',
    yName: '',
    yType: '',
    ymz: 0,
    yxf: 0,
    yxq: '',
  },
  list:[]
});

let { state,list } = toRefs(data);

onMounted(() => {
  MenDainShow2()
})
//下拉框
function MenDainShow2()
{
  axios({
    url:'https://localhost:7127/api/MenDian/MenDianShow2',
    method:'get'
  })
  .then(res=>{
    list.value=res.data.data
  })
  .catch(err=>{
    console.log(err)
  })
}




//优惠劵添加1
function YHJAdd() {
  let obj = {
    mId:state.value.mId,
    yName: state.value.yName,
    yType: state.value.yType,
    ymz:state.value.ymz,
    yxf:state.value.yxf,
    yxq:state.value.yxq,
    ycTume:new Date(),
    yzt: '1',
    yIsDeleted: '0'
  };
  if(obj.mId=="0")
  {
    alert('请选择门店')
    return
  }
  if(obj.yName=="")
  {
    alert("优惠劵名称不能为空")
    return
  }
  if(obj.yType=="")
  {
    alert("优惠劵类型不能为空")
    return
  }
  axios({
    url: 'https://localhost:7127/api/YHJ/YHJAdd',
    method: 'post',
    data: obj,
  })
    .then((res) => {
      if(res.data.data==-1)
    {
      alert('优惠劵名称重复');
        return;
    }
       else if (res.data.data > 0) {
        alert('优惠劵添加成功');
        router.push('/YHJShow');
      } else {
        alert('优惠劵添加失败');
        return;
      }
    })
    .catch((err) => {
      console.log(err);
    });
}

function YHJShow() {
  router.push('/YHJShow');
}



</script>

<style scoped></style>
